package enumTest;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import static java.lang.Class.*;

public enum EnumTest {
    //枚举的对象
    RED,BLACK("黑色",19),GREEN,WHITE;

    //枚举对象的变量
    String color;
    int number;

    //构造方法
    private EnumTest(String color,int number) {
        this.color=color;
        this.number=number;
    }
    private EnumTest(){

    }

    public static void main(String[] args) {

    }
    public static void main4(String[] args) {
        //用反射去调用枚举有参数的构造方法
        try {
            Class<?> c1 = forName("enumTest.EnumTest");
            Constructor<?> constructor = c1.getDeclaredConstructor(String.class, int.class,String.class, int.class);
            constructor.setAccessible(true);
            EnumTest enmuTest=(EnumTest) constructor.newInstance("wzj",19);
            System.out.println(enmuTest);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }


    }
    public static void main3(String[] args) {
        EnumTest enumTest=EnumTest.valueOf("BLACK");
        EnumTest enumTest1=EnumTest.valueOf("RED");
        System.out.println(enumTest1.compareTo(enumTest));
        System.out.println(BLACK.compareTo(RED));

    }
    public static void main2(String[] args) {
        EnumTest enumTest=EnumTest.valueOf("BLACK");
        System.out.println(enumTest);
    }
    public static void main1(String[] args) {
        EnumTest[] arr=EnumTest.values();
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]+" 索引:"+arr[i].ordinal());
        }
    }
}
